/**
 * Created by Administrator on 2018/1/18.
 */

layui.use(['jquery', 'laydate','form','layer','laypage','table'], function(){
    var $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer,
        table = layui.table,
        laypage = layui.laypage;


    laydate.render({
        elem: '#start'//指定元素


    });

    laydate.render({
        elem: '#end' //指定元素

    });

    var under_config = {
        elem: '#proxyuser',
        url:"/proxy/proxyuser",
        method:'post',
        cellMinWidth:80,
        cols:  [[
            //标题栏
            {field: 'id', title: '代理id',align: 'center'},
            {field: 'name', title: '代理姓名',align: 'center'},
            {field: 'phone', title: '手机号码',align: 'center'},
            {field: 'email', title: '邮箱',align: 'center'},
            {field: 'rankid', title: '代理等级',align: 'center'},
            {field: 'rate', title: '佣金分成比例',align: 'center'},
            {field: 'register_time', title: '注册时间',align: 'center'},
            {field: 'remark', title: '备注',align: 'center',width: 150},
            {fixed: 'right', align:'center', title: '操作',toolbar: '#barDemo',width: 250}
        ]],
        //data:data.data,
        page: true,
        limit: 10,
        height: 680,
        // done: function(res, curr, count){
        //
        //     layer.closeAll();
        //     $('#pid').html(res.extra.select_list);
        //     $('#uid').html(res.extra.customer_list);
        //     $('#table_vol').html(res.extra.table_html);
        //     form.render('select');
        // }
    };

    //方法级渲染-默认显示入金记录
    var ins1 = table.render(under_config);

    //表格搜索和重载
    var $ = layui.$, active = {
        reload: function(){
            var index = layer.msg('数据加载中,请耐心等待...', {
                icon: 16,shade: 0.5,time:15000
            });

            var demoReload = $('#test6');
            var uid = $('#uid').val();
            var pid = $('#pid').val();
            ins1.reload({
                where: {
                    //根据MT4搜索
                    pid:pid,
                    proxy_show:proxy_show,
                    customer_show:customer_show,
                    uid:uid,
                    status:status,
                    time: demoReload.val()
                }
            });
        },
        export:function(){
            var time = $('#test6').val();
            var keyword = $('#keyword').val();
            var ticket = $('#ticket').val();
            var pid = $('#pid').val();

            window.location.href = '/user/count_export?pid='+pid+'&keyword='+keyword+'&ticket='+ticket+'&time='+time+'&show='+show+'&rand_id='+new Date().getTime();
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(proxyuser)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        //console.log(data);

        if(data.rate.replace('%',"")>0){
            var first =2;
        }else{
            var first =1;

        }



        if(layEvent === 'set'){ //修改佣金设置
            //do somehing
            layer.open({
                type:2
                ,area:['500px', '300px']
                ,resize:false
                ,btn:'确认'
                ,shade:'0.5'
                ,title:"返佣设置"
                ,content: '/user/brokerageset?first='+first+'&proxyid='+data.id
                ,success:function(layero, index){
                    var body = layer.getChildFrame('body', index);
                    //$form = body.find('#set-form');
                    $input = body.find('input[name=rate]');
                    //console.log($input.val());

                }
                ,yes:function () {
                    //$form_data = $form.serializeArray();
//                $.each($form_data,function (idx,obj) {
//                    if(obj.value=='' ||obj.value==undefined || obj.value<0){
//                        layer.alert('所有的产品都要设置返佣金额');
//                        return false;
//                    }
//
//                });
                    var $rate = $input.val();
                    if($rate=='' ||$rate==undefined || $rate<0){
                        layer.alert('请输入正确的数值，返佣比例为0到100');
                        return false;
                    }

                    $.post('/user/brokerageset?first='+first+'&proxyid='+data.id,{rate:$rate},function (result) {
                        if(result.code==200)
                        {
                            layer.alert('添加成功',function () {
                                //window.location.href='/user/brokerageset?first=1&proxyid='+proxyid;
                                window.location.reload();
                            })
                        }else{
                            layer.alert(result.msg);
                        }
                    },'json');
                }
            });
        }
    });

});
